package higherfunction;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @Description: 高阶函数-简单数据流
 * @Author: tfq
 * @Date: 2025-02-15 17:14
 */
public class SimpleStream<T> {

    public static void main(String[] args) {
        List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        SimpleStream.of(list)
                .filter(value -> value % 2 != 0)
//                .map(value -> value * 2)
                .forEach(System.out::println);
    }

    public SimpleStream<T> filter(Predicate<T> predicate) {
        List<T> result = new ArrayList<>();
        for (T t : collection) {
            if (predicate.test(t)) {
                result.add(t);
            }
        }
        return new SimpleStream<>(result);
    }

    public <R> SimpleStream<R> map(Function<T, R> function) {
        List<R> result = new ArrayList<>();
        for (T t : collection) {
            result.add(function.apply(t));
        }
        return new SimpleStream<>(result);
    }

    public void forEach(Consumer<T> consumer) {
        for (T t : collection) {
            consumer.accept(t);
        }
    }


    public static <T> SimpleStream<T> of(Collection<T> collection) {
        return new SimpleStream<>(collection);
    }

    private Collection<T> collection;

    private SimpleStream(Collection<T> collection) {
        this.collection = collection;
    }
}